package com.zrfintech.manage.controller.mg;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zrfintech.domain.MgPageConfig;
import com.zrfintech.domain.MgPageConfigOption;
import com.zrfintech.manage.vo.response.AmisOption;
import com.zrfintech.service.MgPageConfigOptionRepo;
import com.zrfintech.service.MgPageConfigRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/webapi/pageConfigOption")
public class MgPageConfigOptionController {

    @Autowired
    private MgPageConfigOptionRepo pageConfigOptionRepo;

    @Autowired
    private MgPageConfigRepo pageConfigRepo;

    @RequestMapping("/getAllPageConfigOptionTree")
    public Object getAllPageConfigOptionTree() {
        List<MgPageConfigOption> allMenuList = pageConfigOptionRepo.getAll();
        Map<String, List<MgPageConfigOption>> collect = allMenuList.stream()
                .collect(Collectors.groupingBy(MgPageConfigOption::getPageCode
        ));
        List<AmisOption> options = Lists.newArrayList();
        for (String pageCode : collect.keySet()) {
            MgPageConfig mgPageConfig = pageConfigRepo.getByPageCode(pageCode);
            if (mgPageConfig == null) {
                continue;
            }
            AmisOption amisOption = new AmisOption(mgPageConfig.getPageName(), pageCode);
            List<MgPageConfigOption> pageConfigOptionList = collect.get(pageCode);
            List<AmisOption> children = Lists.newArrayList();
            for (MgPageConfigOption pageConfigOption : pageConfigOptionList) {
                AmisOption amisOptionChildren = new AmisOption(pageConfigOption.getOptionName(),
                        pageConfigOption.getOptionCode());
                children.add(amisOptionChildren);
            }
            amisOption.setChildren(children);
            options.add(amisOption);
        }
        Map result = Maps.newHashMap();
        result.put("options", options);
        return result;
    }
}
